package com.fcgzs.pay.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.fcgzs.pay.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: ljj
 * @Description:
 */
@Service
public class AliPayServiceImpl implements AliPayService {

    @Autowired
    private AlipayClient alipayClient;


    /**
     * 预下单
     *
     * @param trade_out_no 订单编号 注意：不允许重复
     * @param total_fee    应支付金额（单位：分）
     * @return
     */
    @Override
    public Map createNative(String trade_out_no, String total_fee) {
        Map resultMap = new HashMap();

        //分转元  除法
        long total_fee_long = Long.parseLong(total_fee);
        BigDecimal total_fee_big = new BigDecimal(total_fee_long);
        BigDecimal cs = new BigDecimal(100L);
        BigDecimal money = total_fee_big.divide(cs);


        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest (); //创建API对应的request类
        request . setBizContent ( "{"   +
                "\"out_trade_no\":\""+trade_out_no+"\"," + //商户订单号
                "\"total_amount\":\""+money.doubleValue()+"\","   +
                "\"subject\":\"Iphone6 16G\","   +
                "\"store_id\":\"NJ_001\","   +
                "\"timeout_express\":\"90m\"}" ); //订单允许的最晚付款时间
        try {
            AlipayTradePrecreateResponse response = alipayClient.execute (request);
            System.out.println( response.getBody ());
            //根据response中的结果继续业务逻辑处理
            if (response.getCode().equals("10000")) {
                resultMap.put("out_trade_no",response.getOutTradeNo());
                resultMap.put("total_fee",money.doubleValue());
                resultMap.put("qrCode",response.getQrCode());
            }

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 查询交易状态
     *
     * @param out_trade_no 订单编号
     * @return
     */
    @Override
    public Map queryStatus(String out_trade_no) {
       Map resultMap = new HashMap();


        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();//创建API对应的request类
        request.setBizContent("{" +
                "\"out_trade_no\":\""+out_trade_no+"\"," +
                "\"trade_no\":\"\"}"); //设置业务参数
        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);//通过alipayClient调用API，获得对应的response类
            System.out.println(response.getBody());
            //根据response中的结果继续业务逻辑处理
            if (response.getCode().equals("10000")) {
                resultMap.put("out_trade_no",response.getOutTradeNo());    //订单编号
                resultMap.put("tradeNo",response.getTradeNo());    //支付宝平台产生的交易流水号
                resultMap.put("status",response.getTradeStatus());    //交易状态
            }

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }


        return resultMap;
    }
}
